import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.util.*;
import org.math.plot.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import org.jfree.data.*;
import org.jfree.data.statistics.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;

public class ReviewDetection{
	public final static String biz_file = "C:\\Users\\Administrator\\Desktop\\research\\Yelp\\yelp_business.json";
	public final static String user_file = "C:\\Users\\Administrator\\Desktop\\research\\Yelp\\yelp_user.json";
	public final static String check_file = "C:\\Users\\Administrator\\Desktop\\research\\Yelp\\yelp_checkin.json";
	public final static String review_file = "C:\\Users\\Administrator\\Desktop\\research\\Yelp\\yelp_review.json";
	ArrayList<String> biz_list; 
	Hashtable<String,Integer> biz_review_count = new Hashtable<String,Integer>();
	long reviewNum;
	long totalLength;
	
	public ReviewDetection(){
		JSONParser myParser = new JSONParser();
 		BufferedReader br = null;
 		biz_list = new ArrayList();
 		
 		try {
 			br = new BufferedReader(new FileReader(biz_file));
 			String bizLine="";
 			while((bizLine = br.readLine())!=null){
 				Object obj = myParser.parse(bizLine);
 	 
 				JSONObject jsonObject = (JSONObject) obj;
 				//String name = (String) jsonObject.get("name");
 				
 				ArrayList<String> list = (ArrayList)jsonObject.get("categories");
 				// Get all restaurants in business file and save the id in the biz_list
 				if(list.contains("Restaurants")) biz_list.add((String)jsonObject.get("business_id"));
 			}
 			br.close();

 		} catch (FileNotFoundException e) {
 			e.printStackTrace();
 		} catch (IOException e) {
 			e.printStackTrace();
 		} catch (ParseException e) {
 			e.printStackTrace();
 		}catch (Exception e) {
 			e.printStackTrace();
 		}
 		System.out.println("Restaurants #: "+biz_list.size());
 		reviewNum = 0;
 		totalLength = 0;
 		
 		ArrayList<Double> lenList = new ArrayList<Double>();
 		biz_review_count = new Hashtable<String,Integer>();
 		
 		try {
 			br = new BufferedReader(new FileReader(review_file));
 			String reviewLine="";
 			while((reviewLine = br.readLine())!=null){
 				Object obj = myParser.parse(reviewLine);
 	 
 				JSONObject jsonObject = (JSONObject) obj;
 				//String name = (String) jsonObject.get("name");
 				
 				if( biz_list.contains((String)jsonObject.get("business_id"))) {
 					reviewNum++;
 					StringTokenizer st = new StringTokenizer(((String)jsonObject.get("text"))," ");
 					totalLength += st.countTokens();
 					lenList.add((double)st.countTokens());
 					if( biz_review_count.contains((String)jsonObject.get("business_id"))){
 						biz_review_count.put((String)jsonObject.get("business_id"), biz_review_count.get((String)jsonObject.get("business_id"))+1);
 					}
 					else{
 						biz_review_count.put((String)jsonObject.get("business_id"), 1);
 					}
 				}
 			}
 			br.close();

 		} catch (FileNotFoundException e) {
 			e.printStackTrace();
 		} catch (IOException e) {
 			e.printStackTrace();
 		} catch (ParseException e) {
 			e.printStackTrace();
 		}
 		catch (Exception e) {
 			e.printStackTrace();
 		}
 		Double[] x = lenList.toArray(new Double[lenList.size()]);
 		double[] reviewLen = new double[x.length];
 		for(int i = 0 ; i < reviewLen.length; i++){
 			reviewLen[i] = x[i].doubleValue();
 		}
 		
 		double[] y = new double[biz_list.size()];
 		for(int i = 0 ; i < biz_list.size();i++){
 			if( biz_review_count.contains((String)biz_list.get(i))==false )
 				y[i] = 0;
 			else{
 				System.out.print("Found ");
 				y[i] = biz_review_count.get((String)biz_list.get(i)).doubleValue();
 				System.out.println(y[i]);
 			}
 		}
 		
 		 
 		HistogramDataset myHist = new HistogramDataset();
 		myHist.setType(HistogramType.FREQUENCY);
 		myHist.addSeries("Word Length", reviewLen, 20);
 		JFreeChart myChart = ChartFactory.createHistogram("Review Length Histogram", "Length", "Num", myHist,  PlotOrientation.VERTICAL, false, false, false);
 				//ChartFactory.createHistogram("Review Length Histogram", "Length", "Num", myHist , PlotOrientation.VERTICAL, false,false,false);
 		
 		
 		HistogramDataset reviewCountHist = new HistogramDataset();
 		myHist.setType(HistogramType.FREQUENCY);
 		myHist.addSeries("Review Count", y, 100);
 		JFreeChart reviewCountChart = ChartFactory.createHistogram("Review Length Histogram", "Reviews", "Num", reviewCountHist,  PlotOrientation.VERTICAL, false, false, false);
 	
 		
 		System.out.println("Reviews #: "+reviewNum);
 		System.out.println("Ave length: "+totalLength/reviewNum);
 		
 		JFrame frame = new JFrame("review length panel");
       	ChartPanel myPanel = new ChartPanel(myChart);
       	frame.add(myPanel);
       	frame.setSize(500, 500);
       	frame.setVisible(true);
       	
       	JFrame frame2 = new JFrame("review count panel");
       	ChartPanel myPanel2 = new ChartPanel(reviewCountChart);
       	frame2.add(myPanel2);
       	frame2.setSize(500, 500);
       	frame2.setVisible(true);
       	
	}
	
	
	
     public static void main(String[] args) {
    	new ReviewDetection();
     }
}